﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using MGBOMnt = Texfina.BOMnt.Mg;
using SYBOQry = Texfina.BOQry.Sy;

namespace MG
{
   public partial class wfTablaGenMnt : EditPage
   {

      public const int _GRDLIST_IDCONREMU = 3;
      public const int _GRDLIST_STINHABILITA = 6;
      public const int _GRDLIST_STANULADO = 7;


      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();
      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = (FormEditAction)Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
         this.SetInitialTitle("Tabla General");
      }

      public override void LoadListControls()
      {

      }

      public override void DataToControls()
      {
         ETablaGen objE = new ETablaGen();
         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.TablaGen));

         objE.IdTabla = Request.QueryString["idTabla"];
         objE = (ETablaGen)objBO.GetByKey(objE);


         if (objE != null)
         {

            WebHelper.EntityToControl(objE, "IdTabla", txtidTabla);
            WebHelper.EntityToControl(objE, "DsTabla", txtdsTabla);

         }

         ETablaGenDet objEDet = new ETablaGenDet();
        
         MGBOQry.ITablaGen objBOTablaGen = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.TablaGen>(typeof(MGBOQry.TablaGen));

         objEDet.IdTabla = Request.QueryString["idTabla"];
         DataTable dt = objBOTablaGen.GetByParentKeyTablaGenDet(objEDet);

         this.GridSetDataBinding(grdList, dt);
        

      }

      public override void RegisterGeneralScripts()
      {
         txtdsTabla.Attributes.Add("readonly", "true");

         StringBuilder sb = new StringBuilder();

         sb.AppendLine("function validateEditForm(key){");

         sb.AppendLine("var varRpta = isEmptyControl('" + this.txtdsTabla.ClientID + "','Ingrese Descripción de Tabla');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         //sb.AppendLine("var varRpta = isEmptyControl('" + this.txtdsTabla.ClientID + "','Ingrese Numero de Proceso Anual');");
         //sb.AppendLine("if(varRpta==true)");
         //sb.AppendLine("return !varRpta;");

         sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
         sb.AppendLine("}");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);
      }

      public void RegisterEditPopupScripts()
      {
         //StringBuilder sb = new StringBuilder();

         ////sb.AppendLine("var varRet = modalDialog('../RH/wfConcepRemu.aspx',725,400);");
         ////sb.AppendLine("if(varRet==null)");
         ////sb.AppendLine("return false;");
         ////sb.AppendLine("ctl = document.getElementById('" + this.hdnidConRemu.ClientID + "');");
         ////sb.AppendLine("ctl.value = varRet[0];");
         ////sb.AppendLine("ctl = document.getElementById('" + this.txtidConRemu.ClientID + "');");
         ////sb.AppendLine("ctl.value = varRet[1];");
         ////sb.AppendLine("return false;");
         ////this.txtidConRemu.OnClientClick = sb.ToString();

         //ScriptManager.RegisterStartupScript(this, this.GetType(), "", sb.ToString(), true);

         StringBuilder sb2 = new StringBuilder();

         sb2.AppendLine("function validate" + this.bttOkPopup.ID + "(){");
         //sb.AppendLine("debugger;");
         //if (this.GridEditAction == GridEditAction.AddNew)
         //{
         //   sb2.AppendLine("varRpta = gridFindCellValueInRow('" + grdList.ClientID + "',new Array('" + _GRDLIST_IDCONREMU + "'), new Array(document.getElementById('" + this.hdnidConRemu.ClientID + "').value),'Ya existe el concepto de Remuneracion');");
         //   sb2.AppendLine("if(varRpta==true)");
         //   sb2.AppendLine("return !varRpta;");
         //}

         sb2.AppendLine("varRpta = isEmptyControl('" + this.txtdsDescrip.ClientID + "','Ingrese Descripción .');");
         sb2.AppendLine("if(varRpta==true)");
         sb2.AppendLine("return !varRpta;");
         sb2.AppendLine("return true;}");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "validatePopup", sb2.ToString(), true);

         this.bttOkPopup.OnClientClick = "return validate" + bttOkPopup.ID + "();";
         this.bttCancelPopup.OnClientClick = "$find('" + this.mdlPopup.ClientID + "').hide();return false;";

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_SAVE:
            case ConstMenuToolbarValue.BUTTON_SAVENEW:
               if (this.FormEditAction != FormEditAction.Delete)
                  this.SaveData(e.Item.Value);
               else
                  this.DeleteData();
               break;

            case ConstMenuToolbarValue.BUTTON_EXIT:
               this.Response.Redirect("wfTablaGenBnd.aspx");
               break;
         }

      }

      public override void SaveData(String key)
      {
         if (this.ValidateData() == false)
            return;

         IEntityBase objE = (IEntityBase)this.CreateEntitySet();

         IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(MGBOMnt.TablaGen));

         String[] strRet = objPro.UpdateData(objE);
         String strMsg = "";

         switch (this.FormEditAction)
         {

            case FormEditAction.AddNew:
            case FormEditAction.Copy:
               strMsg = "Se ha registrado la Tabla : " + strRet[0].ToString();
               break;
            default:
               strMsg = "Se ha modificado la Tabla : " + txtidTabla.Text + "  " + txtdsTabla.Text;
               break;
         }
         this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "wfTablaGenBnd.aspx");

      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         this.EditRowIndex = Convert.ToInt32(e.CommandArgument);

         switch (e.CommandName)
         {
            case ConstGridRowCommand.GRIDEDIT:
               this.lblTitlePopup.Text = "Detalle Tabla (Editar)";
               this.GridEditAction = GridEditAction.Edit;
               this.mdlPopup.Show();
               this.LoadDataEditPopup(grdList, this.EditRowIndex);
               this.RegisterEditPopupScripts();
               break;
            case ConstGridRowCommand.GRIDDELETE:
               this.lblTitlePopup.Text = "Detalle Tabla (Dar de Baja)";
               this.GridEditAction = GridEditAction.Delete;
               this.mdlPopup.Show();
               this.LoadDataEditPopup(grdList, this.EditRowIndex);
               this.RegisterEditPopupScripts();
               break;
         }
      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         //if (e.Row.RowType == DataControlRowType.DataRow)
         //{
         //   if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
         //   { e.Row.ForeColor = System.Drawing.Color.Red; }

         //   CheckBox chk = (CheckBox)e.Row.FindControl("chkstDesHabilita");
         //   if (e.Row.Cells[_GRDLIST_STINHABILITA].Text == ConstFlagEstado.ACTIVADO)
         //   {
         //      chk.Checked = true;
         //   }

         //}

      }

      protected void mnuGrid_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_NEW:
               this.GridEditAction = GridEditAction.AddNew;
               this.lblTitlePopup.Text = "Planilla (Nuevo)";
               this.mdlPopup.Show();
               this.ClearEditPopup();
               this.LoadDataEditPopup(grdList, -1);
               this.RegisterEditPopupScripts();
               break;
         }

      }

      protected void bttOkPopup_Click(object sender, EventArgs e)
      {

         DataTable dt = (DataTable)this.DataSource;
         DataRow dr = null;
         Object objCodigo = null;
         int intCodigo = 0;

         if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
         {
           

            objCodigo = dt.Compute("MAX(idCodigo)", "");
            if (objCodigo == null || objCodigo.ToString() == "")
               intCodigo = 0;
            else
               intCodigo = Convert.ToInt32(objCodigo);
            intCodigo += 1;
            dr = dt.NewRow();
            dr["idCodigo"] = intCodigo.ToString("000");
         
         }
         else
         {
            dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
         }

         if (this.GridEditAction != GridEditAction.Delete)
         {
            dr["idTabla"] = Request.QueryString["idTabla"];
            dr["dsDescrip"] = this.txtdsDescrip.Text.ToString();
            dr["dsAbrev"] = this.txtdsAbrev.Text.ToString();
            dr["idVar"]="";
            dr["idCodAlter"]="";
            dr["mtValor"] = 0.00;
   

         }
         else
         {
            //dr["stAnulado"] = ConstFlagEstado.ACTIVADO;
         }

         if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
         {
            dt.Rows.Add(dr);
         }


         this.GridSetDataBinding(grdList, dt);
      }

      public override void LoadDataEditPopup(GridView grid, int rowIndex)
      {
         this.ClearEditPopup();
         this.LockControlsPopup();

         DataRow dr = this.GridGetRowFromDataKey(this.DataSource, grdList, rowIndex);

         if (dr == null)
            return;

         WebHelper.RowToControl(dr, "dsDescrip", txtdsDescrip);
         WebHelper.RowToControl(dr, "dsAbrev", txtdsAbrev);


      }

      public new void ClearEditPopup()
      {
         //this.chkstHabilitado.Checked = false;
         //this.txtnuSecuen.Text = "0";
         //this.txtidConRemu.Text = "";
         //this.hdnidConRemu.Value = "";

      }

      public void LockControlsPopup()
      {
         //switch (this.GridEditAction)
         //{
         //   case GridEditAction.AddNew:
         //   case GridEditAction.Edit:
         //      this.txtfeBajaDH.Enabled = false;
         //      this.ddlidMotivoBajaDH.Enabled = false;
         //      break;
         //   case GridEditAction.Delete:
         //      this.txtfeBajaDH.Enabled = true;
         //      this.ddlidMotivoBajaDH.Enabled = true;
         //      this.txtdsApePaternoDH.Enabled = false;
         //      this.txtdsApeMaternoDH.Enabled = false;
         //      this.txtdsNombresDH.Enabled = false;
         //      this.ddlidTipoDocDH.Enabled = false;
         //      this.txtnuDocDH.Enabled = false;
         //      this.txtfeNacimientoDH.Enabled = false;
         //      this.ddlidGeneroDH.Enabled = false;
         //      this.ddlidVincFamDH.Enabled = false;
         //      this.ddlidVincFamDH.Enabled = false;
         //      this.ddlidTipoDocVincFamDH.Enabled = false;
         //      this.txtnuDocVincFamDH.Enabled = false;
         //      this.txtfeConcepDH.Enabled = false;
         //      this.txtnuCodLDN.Enabled = false;
         //      this.txtnuTelefonoDH.Enabled = false;
         //      this.txtdsEmailDH.Enabled = false;
         //      this.txtdsUbigeo.Enabled = false;
         //      this.ddlidTipoVia.Enabled = false;
         //      this.txtdsNombreVia.Enabled = false;
         //      this.txtnuVia.Enabled = false;
         //      this.txtnuDpto.Enabled = false;
         //      this.txtnuInterior.Enabled = false;
         //      this.txtdsManzana.Enabled = false;
         //      this.txtdsLote.Enabled = false;
         //      this.txtdsKilometro.Enabled = false;
         //      this.txtdsBlock.Enabled = false;
         //      this.txtdsEtapa.Enabled = false;
         //      this.ddlidTipoZona.Enabled = false;
         //      this.txtdsNombZona.Enabled = false;
         //      this.txtdsReferen.Enabled = false;
         //      this.txtdsUbigeo2.Enabled = false;
         //      this.ddlidTipoVia2.Enabled = false;
         //      this.txtdsNombreVia2.Enabled = false;
         //      this.txtnuVia2.Enabled = false;
         //      this.txtnuDpto2.Enabled = false;
         //      this.txtnuInterior2.Enabled = false;
         //      this.txtdsManzana2.Enabled = false;
         //      this.txtdsLote2.Enabled = false;
         //      this.txtdsKilometro2.Enabled = false;
         //      this.txtdsBlock2.Enabled = false;
         //      this.txtdsEtapa2.Enabled = false;
         //      this.ddlidTipoZona2.Enabled = false;
         //      this.txtdsNombZona2.Enabled = false;
         //      this.txtdsReferen2.Enabled = false;
         //      break;
         //}
      }

      public override void DeleteData()
      {
         //IEntityBase objE = (IEntityBase)this.CreateEntitySet();
         //IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Personal));
         //objPro.DeleteData(objE);

         //this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "Se anulo el Personal  : " + txtidPersonal.Text, "wfPersonalBnd.aspx");

      }

      public override Object CreateEntitySet()
      {

         ETablaGen objE = new ETablaGen();

         if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
            objE.EntityState = EntityState.Added;
         else
         {
            IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.TablaGen));

            objE.IdTabla = Request.QueryString["idTabla"];
            objE = (ETablaGen)objBO.GetByKey(objE);

            if (this.FormEditAction == FormEditAction.Edit)
               objE.EntityState = EntityState.Modified;
            if (this.FormEditAction == FormEditAction.Delete)
               objE.EntityState = EntityState.Deleted;

         }



         WebHelper.ControlToEntity(txtidTabla, objE, "IdTabla");
         WebHelper.ControlToEntity(txtdsTabla, objE, "DsTabla");
         //WebHelper.ControlToEntity(txtnuProcAnual, objE, "NuProcAnual");

         if (this.FormEditAction == FormEditAction.Delete)
         {
            //objE.StAnulado = ConstFlagEstado.ACTIVADO;
         }
         else
         {
            //objE.StAnulado = ConstFlagEstado.DESACTIVADO;
         }

         DataTable dt = (DataTable)this.DataSource;

         //this.ClearDeletedRow(this.DataSource, "stAnulado");

         IList<ETablaGenDet> colE = new List<ETablaGenDet>();

         for (int intTypeUpd = 0; intTypeUpd <= 1; intTypeUpd++)
         {
            DataTable dtChange = null;
            if (intTypeUpd == 0)
               dtChange = dt.GetChanges(DataRowState.Added);
            else
               dtChange = dt.GetChanges(DataRowState.Modified);


            if (dtChange != null)
            {
               foreach (DataRow drDet in dtChange.Rows)
               {
                  ETablaGenDet objEDet = DataHelper.CopyDataRowToEntity<ETablaGenDet>(drDet, typeof(ETablaGenDet));

                  if (drDet.RowState == DataRowState.Added)
                     objEDet.EntityState = EntityState.Added;

                  if (drDet.RowState == DataRowState.Modified)
                  {
                     //if (objEDet.StAnulado == ConstFlagEstado.DESACTIVADO)
                        objEDet.EntityState = EntityState.Modified;
                     //else
                     //   objEDet.EntityState = EntityState.Deleted;
                  }

                  colE.Add(objEDet);
               }
            }
         }

         ETablaGenSet objESet = new ETablaGenSet();

         objESet.TablaGen = objE;
         objESet.ColTablaGen = colE;

         return objESet;
      }

   }
}